Repository navigation
Conversation
…drops garbageCollect(sync=true) ran a full JS GC then returned. JS finalizers freed ArrayBuffer backing stores and WTF::StringImpl bodies into libpas page caches, and Bun-owned bytes into mimalloc thread heaps, but neither allocator was told to decommit. RSS stayed at peak (hundreds of MB of 'overhead' above the now-empty JS heap) until libpas's background scavenger / mimalloc's purge timer eventually ran — and under sustained allocation churn they never catch up, so RSS ratchets toward OOM while heapUsed stays flat. After runGC(true), now call WTF::releaseFastMallocFreeMemory() (bmalloc::api::scavenge -> pas_scavenger_run_synchronously_now) followed by mi_collect(true) so both allocators madvise freed pages back to the OS. On the churn repro from #21560, post-GC RSS released goes from 0% to ~95%. Fixes #21560 Fixes #27514
WalkthroughThis PR updates Bun.gc(true) to perform synchronous allocator page scavenging and mimalloc cleanup after the JSC synchronous GC, and adds a Linux-only test that verifies RSS pages are returned to the OS. ChangesGarbage Collection Memory Scavenging
🚥 Pre-merge checks | ✅ 4✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. Comment |
|
Updated 7:47 PM PT - May 12th, 2026
❌ @robobun, your commit 73c9d5c has 3 failures in
🧪 To try this PR locally: bunx bun-pr 30590That installs a local version of the PR into your bun-30590 --bun |
|
Found 5 issues this PR may fix:
🤖 Generated with Claude Code |
|
This PR may be a duplicate of:
🤖 Generated with Claude Code |
Status — ready for maintainer reviewAll review threads resolved. Gate verified locally: CI
The new test is Linux-gated ( The Windows failures across both runs are pre-existing flakes unrelated to this diff:
None touch GC, mimalloc, libpas, or the new test file. This diff is green; needs a maintainer to merge past the Windows flake. |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@test/js/bun/util/bun-gc-releases-rss.test.ts`:
- Around line 50-53: The test currently computes pct = growth > 0 ? (released /
growth) * 100 : 100 which masks cases where growth === 0; change the logic in
the test around the variables peak, start, final, growth and pct to explicitly
assert/fail when growth is 0 before computing pct (e.g., throw or use an
assertion that growth > 0), then compute pct = (released / growth) * 100 and
continue the existing reclamation check so the test cannot pass as a false
positive when RSS growth is zero.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: ASSERTIVE
Plan: Pro
Run ID: 8e8103ea-e543-4823-bac5-bb77493f2dc5
📒 Files selected for processing (4)
src/jsc/VirtualMachine.zigsrc/jsc/WTF.zigsrc/jsc/bindings/wtf-bindings.cpptest/js/bun/util/bun-gc-releases-rss.test.ts
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@test/js/bun/util/bun-gc-releases-rss.test.ts`:
- Around line 53-59: The test currently logs rounded display strings (using
toFixed) for start/peak/final/growth/pct and then later asserts on those rounded
values, causing flaky boundary failures; change the assertions to use the
original numeric variables start, peak, final, growth and pct (not the toFixed
strings) in the expectation checks (the console.log with toFixed should remain
only for human-readable output) — update any assertions that reference the JSON
output or Number(...) of the rounded fields (used around the assertions near the
expect calls referencing growth and releasedPct) to compare against the raw
numeric variables growth and pct instead.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: ASSERTIVE
Plan: Pro
Run ID: 880e563c-31ae-4b29-8e88-f4314260e562
📒 Files selected for processing (1)
test/js/bun/util/bun-gc-releases-rss.test.ts
|
Closing: this PR predates the Rust rewrite and modifies source files that no longer exist on If the underlying issue is still present, it will need a fresh fix against the current tree. |
Problem
Bun.gc(true)(andglobal.gc()via--expose-gc) ran a full sync JS GC but never told the native allocators to return their freed pages to the OS. After GC:WTF::StringImplbodies sat in libpas page caches waiting for the backgroundpas_scavengerthreadbun.default_allocator) sat on mimalloc per-thread heaps waiting for the segment-purge delaySo
process.memoryUsage().rssstayed at peak even though the JS heap was empty. Under sustained allocation churn (SSR, long-running daemons, child processes sending IPC) the scavenger/purge never catches up, and RSS ratchets toward OOM whileheapUsedlooks flat. This is the mechanism behind #21560, #27514, and the detailed repro at https://github.com/jeremiahrhall/repro-bun-rss.Reproduction
Bun.gc(true)RSS releasedFix
After
runGC(true)inVirtualMachine.garbageCollect:WTF::releaseFastMallocFreeMemory()→bmalloc::api::scavenge()→pas_scavenger_run_synchronously_now()— decommits libpas/Gigacage free pagesmi_collect(true)— forces mimalloc tomadvise(MADV_DONTNEED)its free segmentsBoth are on an already-
@branchHint(.cold)path that the user invokes explicitly, so no hot-path cost.This supersedes #28743 (which only addressed the mimalloc half).
Verification
test/js/bun/util/bun-gc-releases-rss.test.tsruns the churn workload in a subprocess and asserts >50% of peak RSS growth is released byBun.gc(true). Linux-gated because onlyMADV_DONTNEEDdrops pages from RSS immediately; macOS/Windows use lazy reclaim so the runtime fix applies but the RSS measurement doesn't.releasedPct: 0.0→ failreleasedPct: 95.3→ passScope
Note this makes explicit
Bun.gc(true)/global.gc()reclaim RSS. It does not change the async GC path or add any automatic periodic scavenge — workloads that never call sync GC still rely on the background scavenger, so the SSR-under-continuous-load reports (#14389, #16339) are likely helped but not guaranteed closed by this alone.Fixes #21560
Fixes #27514
Fixes #20487
Fixes #15020
Fixes #28741